//@ts-ignore
const {invoke} = window.__TAURI__.tauri//需要在tauri.conf.json中开启全局变量  "withGlobalTauri": true
const elementsByClassName = document.querySelectorAll('.number') as NodeListOf<HTMLInputElement>;
let data: number[] = [];
let tauriHTML = document.querySelector('#header')!;
document.onkeydown = function (e) {    //对整个页面监听
    const keyNum = e.code;       //获取被按下的键值
//判断是否按下了回车键
    if (keyNum.toLowerCase() == "enter") {
        cal()
    }
}
const invoker = () => {
    invoke('get24', {name: data})
        .then((response: any[]) => {
            response.map(x => "<div>" + x + "</div>")
                .forEach(x => tauriHTML.innerHTML += x);
        })
}
const cal=()=>{
    data = [];
    tauriHTML.innerHTML = "";
    elementsByClassName.forEach(x => {
        if (x.value !== "") {
            data.push(parseInt(x.value, 10))
        }
    })
    if (checkNumber(data)) {
        if ((document.querySelector('.numbertarget') as HTMLInputElement).value !== "") {
            invoker()
        } else {
            tauriHTML.innerHTML = "目标值不能为空";
        }
    } else {
        tauriHTML.innerHTML = "请确认输入数字";
    }
}
document.querySelector('#sumbitBotton')!.addEventListener('click', cal)

function checkNumber<T extends number>(data: T[]) {
    for (let i = 0; i < data.length; i++) {
        if (isNaN(data[i])) {
            return false
        }
    }
    return true
}
